<?php

namespace App\Repository;

use App\Exceptions\BusinessException;
use App\Common\Constant;
use App\Common\ResponseCode;
use App\Models\Base\Files;
use App\Models\Cart\CartItem;
use App\Models\Cart\Carts;
use App\Models\Customer\Address;
use App\Models\Goods\AttributesValue;
use App\Models\Goods\FileBind;
use App\Models\Goods\Goods;
use App\Models\Goods\Products;
use App\Models\Role\Role;
use App\Models\Sales\Orders;
use App\Models\User\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
 * 购物车数据仓储类
 *
 * @package App\Http\Repository
 * @author WangBin
 * @mail: <849850352@qq.com>
 */
class CartRepository extends BaseRepository
{
    /**
     * 获取购物车
     *
     * @param int $customerId
     * @param int $fastBuy
     * @throws
     * @return int|mixed
     */
    public function getByCustomerId(int $customerId, int $fastBuy = 0)
    {
        return Carts::query()->where('customer_id', $customerId)->where('fast_buy', $fastBuy)->first();
    }

    /**
     * 获取结算商品
     *
     * @param int $customerId
     * @param int $fastBuy
     * @return array|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getCheckoutGoods(int $customerId, int $fastBuy = 0)
    {
        try {
            // 获取购物车
            $cart = $this->getByCustomerId($customerId, $fastBuy);
            if(is_null($cart)) {
                throw new \Exception('购物车不存在');
            }
            // 获取购物车商品
            return CartItem::query()->where('cart_id', $cart->cart_id)->where('is_selected', 1)->get();
        } catch (\Exception $exception) {
            Log::error("[获取购物车] 失败：{$exception->getMessage()}");
            return null;
        }
    }

    /**
     * 清除已结算购物车商品
     *
     * @param int $customerId
     * @param int $fastBuy
     */
    public function removeCheckoutGoods(int $customerId, int $fastBuy = 0)
    {
        // 获取购物车
        $cart = $this->getByCustomerId($customerId, $fastBuy);
        // 清除已结算商品
        CartItem::query()->where('cart_id', $cart->cart_id)->where('is_selected', 1)->delete();
        // 重新计算购物车
        $this->recount($cart->cart_id);
    }

    /**
     * 重新计算购物车
     *
     * @param int $cartId
     * @return int
     */
    private function recount(int $cartId)
    {
        // 获取购物车
        $cart = Carts::getById($cartId);
        // 获取购物车明细
        $cartItems = CartItem::getItems($cartId);
        $cart->total_qty = 0;
        $cart->total_amount = 0;
        foreach ($cartItems as $cartItem)
        {
            // 购物车总金额
            $cart->total_amount += $cartItem->total_amount;
            // 购物车总数量
            $cart->total_qty += $cartItem->qty_ordered;
        }
        $cart->total_count = $cartItems->count();
        return $cart->save();
    }

    /**
     * 根据客户 Id 获取购物车明细
     *
     * @param int $customerId
     * @param int $fastBuy
     * @param int $isSelected
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getItemsByCustomerId(int $customerId, int $fastBuy = 0, int $isSelected = 0)
    {
        $cart = Carts::getByCustomerId($customerId, $fastBuy);
        return CartItem::getItems($cart->cart_id, $isSelected);
    }

    /**
     * 删除购物车商品
     *
     * @param int $cartId
     * @param array $productIds
     * @return mixed
     */
    public function delete(int $cartId, array $productIds)
    {
        return CartItem::query()->where('cart_id', $cartId)->whereIn('product_id', $productIds)->delete();
    }
}
